<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    /**/
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(title,formatter,min,max) {
      /*title=title?title:'行驶速度';
      formatter=formatter?formatter:'{value}km/h';
      min=min?min:'0';
      max=max?max:'180';*/
      this.chart = echarts.init(this.$el, 'macarons');
      this.option = {
        /*tooltip: {
          formatter: '{a} <br/>{b} : {c}km/h',
          fontSize: 10,
        },*/
        toolbox: {
          feature: {
            restore: {},
            saveAsImage: {}
          }
        },
        series: [
          {
            name: title,
            type: 'gauge',
            radius: '100%' ,
            center:['50%','70%'],
            startAngle : 200,
            endAngle:-20,
            min: min,
            max: max,
            title: {
              fontSize: 14,
            },
            detail: {
              formatter: formatter,
              fontSize: 16,
            },
            data: [{value: 0, name: title}],
            axisLine: {
              lineStyle: {
                width:12,
                color:[[1,'#2c7df6']]
              },
            },
            splitNumber: 6,
            splitLine: {
              length: 0,
              lineStyle:{
                color:'#2c7df6',
                width:4
              }
            },
            axisLabel:{
              distance :20,
              formatter: function (value) {
                return Math.round(value);
              }
            }
          }
        ]
      };
      this.chart.setOption(this.option);
      /*let $this = this;
      setInterval(function () {
        $this.option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
        $this.chart.setOption($this.option, true);
      },2000);*/
    },change(value,min,max){
      this.option.series[0].data[0].value = value;
      if(max!==void 0){
        this.option.series[0].min = min;
        this.option.series[0].max = max;
      }
      this.chart.setOption(this.option, true);
    }
  }
}
</script>
